﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Benefit.ViewModel.Month
{
    public class CompanyMonthHistory
    {
        /// <summary>
        /// 左侧菜单显示信息
        /// </summary>
        public LeftMenuInfos LeftMenuInfos { get; set; }

        public string DateQuery { get; set; }

        public Benefit.Models.Sys.Company Company { get; set; }

        public string QueryYear { get; set; }

        public string QueryMonth { get; set; }

        public string Pdate { get; set; }

        /// <summary>
        /// 小组月总计
        /// </summary>
        public List<Models.Month.GroupMonthSum> GroupMonthSum { get; set; }

        /// <summary>
        /// 公司月总计
        /// </summary>
        public Models.Month.CompanyMonthSum CompanyMonthSum { get; set; }

        /// <summary>
        /// 公司月均值
        /// </summary>
        public Models.Month.CompanyAvgMonth CompanyAvgMonth { get; set; }

        /// <summary>
        /// 公司年均值
        /// </summary>
        public Models.Year.CompanyYearAvg CompanyYearAvg { get; set; }

        /// <summary>
        /// 公司年总计
        /// </summary>
        public Models.Year.CompanyYearSum CompanyYearSum { get; set; }

        /// <summary>
        /// 去年同期
        /// </summary>
        public Models.Month.CompanyMonthSum PreCompanyMonthSum { get; set; }

        /// <summary>
        /// 页面脚本
        /// </summary>
        public string JS { get; set; }


        #region 定义一些使用到的接口
        private Interface.Sys.ICompany ICompany = null;
        private Interface.Sys.ITradeHistory ITradeHistory = null;
        private Interface.Month.IGroupMonthSum IGroupMonthSum = null;
        private Interface.Month.ICompanyMonthSum ICompanyMonthSum = null;
        private Interface.Month.ICompanyAvgMonth ICompanyAvgMonth = null;
        private Interface.Year.ICompanyYearAvg ICompanyYearAvg = null;
        private Interface.Year.ICompanyYearSum ICompanyYearSum = null;
        private Interface.Day.ICompanyDayChange ICompanyDayChange = null;
        #endregion

        public CompanyMonthHistory(int queryDate, DB.DBManager db)
        {
            InitInterface(db);

            this.DateQuery = queryDate.ToString();
            this.Company = ICompany.GetCompany();

            this.QueryYear = queryDate.ToString().Substring(0, 4);
            this.QueryMonth = queryDate.ToString().Substring(4, 2);

            this.GroupMonthSum = IGroupMonthSum.GetGroupMonthSum(Convert.ToInt32(this.QueryMonth), Convert.ToInt32(this.QueryYear));

            this.CompanyMonthSum = ICompanyMonthSum.GetCompanyMonthSum(Convert.ToInt32(this.QueryMonth), Convert.ToInt32(this.QueryYear));

            this.CompanyAvgMonth = ICompanyAvgMonth.GetCompanyAvgMonth(Convert.ToInt32(this.QueryMonth), Convert.ToInt32(this.QueryYear));

            this.CompanyYearAvg = ICompanyYearAvg.GetCompanyAvgYear(Convert.ToInt32(this.QueryYear));

            this.CompanyYearSum = ICompanyYearSum.GetCompanyYearSum(Convert.ToInt32(this.QueryYear));

            int yestermonth = 0;
            int yesteryear = 0;
            if (Convert.ToInt32(this.QueryMonth) == 1)
            {
                yestermonth = 12;
                yesteryear = Convert.ToInt32(this.QueryYear) - 1;
            }
            else
            {
                yestermonth = Convert.ToInt32(this.QueryMonth) - 1;
                yesteryear = Convert.ToInt32(this.QueryYear);
            }

            this.PreCompanyMonthSum = ICompanyMonthSum.GetCompanyMonthSum(yestermonth, yesteryear);

            var thisCompanyDayChanges = ICompanyDayChange.GetCompanyDayChange(Convert.ToInt32(this.QueryMonth), Convert.ToInt32(this.QueryYear));

            var preCompanyDayChanges = ICompanyDayChange.GetCompanyDayChange(yestermonth, yesteryear);

            this.JS += Benefit.Tools.ChartAtManager.ChartManager.DayShouyilv(thisCompanyDayChanges, preCompanyDayChanges);
            this.JS += Benefit.Tools.ChartAtManager.ChartManager.CompanyMonthBenefitString(thisCompanyDayChanges);

            DisposeInterface();
        }

        private void InitInterface(DB.DBManager db)
        {
            #region 初始化接口
            ICompany = new Service.Sys.ServiceCompany(db);
            ITradeHistory = new Service.Sys.ServiceTradeHistory(db);
            IGroupMonthSum = new Service.Month.ServiceGroupMonthSum(db);
            ICompanyMonthSum = new Service.Month.ServiceCompanyMonthSum(db);
            ICompanyAvgMonth = new Service.Month.ServiceCompanyAvgMonth(db);
            ICompanyYearAvg = new Service.Year.ServiceCompanyYearAvg(db);
            ICompanyYearSum = new Service.Year.ServiceCompanyYearSum(db);
            ICompanyDayChange = new Service.Day.ServiceCompanyDayChange(db);
            #endregion
        }
        /// <summary>
        /// 释放接口
        /// </summary>
        private void DisposeInterface()
        {
            #region 释放接口
            ICompany = null;
            ITradeHistory = null;
            IGroupMonthSum = null;
            ICompanyMonthSum = null;
            ICompanyAvgMonth = null;
            ICompanyYearAvg = null;
            ICompanyYearSum = null;
            ICompanyDayChange = null;
            #endregion
        }
    }
}
